<?php
namespace control;
/*
*固定资产分类
*/
class assetCatalogClass extends adIndexClass
{
    private const page  = 'assetManagement'; # 权限
    private const table = 'asset_catalog'; # 数据表
    private const title = '固定资产分类';

    /**
     * 获取当前分类ID的所有子级分类（包括自己）
     * @param $id
     * @return array
     */
    public static function getAllChildTypeId($id): array
    {
        $typeIdArr = [];
        if (empty($id)) {
            return $typeIdArr;
        } else {
            $typeIdArr[] = $id;
            $material_type = query(self::table, "id = '$id'");
            $child = find(array('table' => self::table, 'where' => "pid = '$material_type[id]'"));
            foreach ($child['array'] as $array) {
                $result = self::getAllChildTypeId($array['id']);
                $typeIdArr = array_merge($typeIdArr, $result);
            }
        }
        return array_filter($typeIdArr);
    }

    public static function getCatalogAll($data = [])
    {
        $orderBy = ' ORDER BY list ASC ';
        // 条件
        $data['where'] = $data['where'] ?? "pid='0'";
        // 点击事件
        $data['click'] = $data['click'] ?? 'replaceDetail(this, \'assetCatalog\')';
        // 结果
        $result = find(['table'=>self::table, 'where'=> $data['where']." and coid = '".self::$coid."'". $orderBy]);
        $style = ($data['where'] == "pid='0'") ? '' : ' class="child-node"';
        $temp = "<ul {$style} {$data['where']}>";
        $textcontent_ico = "<span class='textcontent_ico'></span>";
        // 角标
        $lastone = lastone;
        if ($result['array']) {
            foreach ($result['array'] as $value) {
                // 判断下级是否存在
                $data['where'] = " pid='{$value['id']}' ";
                $arrow = "<span class='arrow'  onclick=\"liZoom($(this).parent())\"></span>";
                $has = query(self::table, $data['where'].$orderBy);
                if ($has['id']) {
                    $temp .="<li data-id='{$value['id']}' onclick=\"{$data['click']}\">
                                {$arrow}
                                <span class='textcontent'>{$textcontent_ico}{$value['name']}</span>
                            </li>";
                    $temp .= self::getCatalogAll($data);
                } else {
                    $temp .="<li data-id='{$value['id']}' onclick=\"{$data['click']}\">
                                {$lastone}
                                <span class='textcontent'>{$textcontent_ico}{$value['name']}</span>
                            </li>";
                }
            }
        }
        $temp .= '</ul>';
        return $temp;
    }

    /*编辑分类基本资料-弹出层*/
    public function edit()
    {
        $id = $this->post('id')?:suiji();
        //数据查询
        $info = query(self::table, " id = '".$id."' ");
        $pid = $info['pid'] ?:  ($this->post('pid') ?? 0);
        //表单
        $html = $this->layerEdit([
            "table" => [
                [
                    must."分类名称：" => text(["name" => "name","value" => $info['name'], 'class'=> 'text']),
                ],
                [
                    "排序号：" => text(["name" => "list","value" => $info['list'], 'class'=> 'text']).hidden([ 'name'=>'pid', 'value'=>$pid ]),
                ]
            ]
        ]);
        //返回
        return $this->layer(array("title" => "编辑","height" => 200,"html" => $html));
    }
    /*分类基本信息-异步处理*/
    public function editEdit()
    {
        $id = $this->post('id')?:suiji();
        //赋值
        $field = array(
            'pid' => $this->post('pid') ?? 0,
            "name" => $this->post('name'),   //分类名称
            "list" => $this->post('list'),   //排序号
        );
        $info = query(self::table, " id = '".$id."' ");
        //判断
        if (!$this->power('assetManagement', "edit")) {
            $json['warn'] = "你没有编辑的权限";
        }elseif (empty($field['name'])) {
            $json['warn'] = "请填写分类名称";
        } elseif (sqlNum(self::table, "pid = '$field[pid]' and name = '$field[name]' and coid = '".self::$coid."' and id != '".$id."' ") > 0) {
            $json['warn'] = "该分类已经存在";
        } elseif (!empty($field['list']) && preg_match(checkInteger, $field['list']) == 0) {
            $json['warn'] = "排序号必须为正整数";
        } elseif (empty($id)) {
            $json['warn'] = "分类ID号为空";
        } elseif (empty($info['id'])) {
            $field = array_merge($field, ["id" => $id,'coid'=>self::$coid, 'stid'=>self::$stid]);
            $json['warn'] = insert(["table" => self::table, "field" => $field]);
        } else {
            $json['warn'] = update(["table" => self::table, "field" => $field, "where" => "id = '".$id."'"]);
        }
        if (in_array($json['warn'], array("新增成功","更新成功"))) {
            //添加日志
            $type = mb_substr($json['warn'], 0, 2, 'utf-8');
            $text = $this->control['name'].$type."了一个【".self::title."】：【分类名称】".$field['name'];
            $this->logText(["type" => self::page, "text" => $text]);
            //提示
            $_SESSION['warn'] = $json['warn'];
            $json['warn'] = 2;
        }
        //返回
        return json_encode($json, 320);
    }

    /**
     * 获取分类名
     *
     * @param string $id
     * @return string
     */
    public static function getCatalogName($id): string
    {
        $temp = '';
        if ($id) {
            $info = query(self::table, " id = '$id' ");
            if ($info['pid'] == '0') {
                $temp .= $info['name'];
            } else {
                $temp .= self::getCatalogName($info['pid']) . ' > ' . $info['name'];
            }
        }
        return $temp;
    }
}
